<template>
  <div class="me">
    <input type="text" placeholder="电话" v-model="phone">
    <br>
    <input type="password" placeholder="密码" v-model="pwd">
    <br>
    <span @click="login()">登录</span>
    <span @click="$router.push('/register')">注册</span>
  </div>
</template>

<script>
import {userlogin} from '../utils/api'
export default {
    data(){
        return{
            formData:{
                phone:'',
                pwd:''
            }
        }
    },
    methods:{
      login(){
       if ( /^1[3-9]\d{9}$/.test( this.formData.phone ) == false) {
        alert('请输入合法的手机号');
       }else if(!this.formData.pwd){
        alert('请输入密码')
       }else{
         userlogin(this.formData).then((res)=>{
             if (res.data.code=200) {
                     localStorage.setItem('token',res.data.token);
                     localStorage.setItem('userinfo', JSON.stringify( res.data.userinfo ) );
                     this.$router.push('/')
             }else{
                 alert('登录失败')
             }
             })
       }
         
      }
    }
}
</script>

<style lang="scss" scoped>
.me{
    padding: 30vw 0 0 5vw;
}
.me input{
    width: 50vw;
    height: 8vw;
    margin-bottom: 5vw;
}
</style>